package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.dao.NewsBulletinDao;
import io.renren.entity.Attachment;
import io.renren.entity.News;
import io.renren.vo.Pager;
import io.renren.entity.Topic;
import io.renren.service.NewsBulletinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 新闻公告模块接口
 *
 * @author Administrator
 */
@Service
public class NewsBulletinServiceImpl implements NewsBulletinService {

    @Autowired
    private NewsBulletinDao newsBulletinDao;

    public List<News> listChannelByParent(Integer pid) {
        List<News> list = newsBulletinDao.listChannelByParent(pid);
        return list;
    }


    /**
     * 检索某个栏目的推荐文章，如果cid为空，表示检索全部的文章
     *
     * @param ci
     * @param limit
     * @return
     */
    public Pager<Topic> queryTopic(Integer ci, Integer currentPage, Integer limit) {
        //
        Page<Topic> page = new Page(currentPage,limit);
        Wrapper wrapper = new EntityWrapper().eq("status", 1).eq("cid", ci);
        wrapper = SqlHelper.fillWrapper(page, wrapper);
        wrapper.orderBy("publish_date", false);
        List list = newsBulletinDao.selectPage(page, wrapper);
        // 封装栏目分页的Pager对象
        Pager<Topic> pager = new Pager(page.getTotal(), list);
        return pager;
    }

    /**
     * 根据文章id获取文章对象信息
     *
     * @param id 文章id
     * @return 文章对象
     */
    public Topic load(int id) {
        return newsBulletinDao.load(id);
    }

    @Override
    public List<Attachment> listByTopic(int tid) {
        return newsBulletinDao.listByTopic(tid);
    }
}
